

import gov.nih.mipav.model.structures.ModelImage;
import gov.nih.mipav.model.structures.ModelStorageBase;
import gov.nih.mipav.view.ViewJFrameImage;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JButton;

public class Segment extends Method implements Drawable, ActionListener,
		MethodRunnable, Runnable {

	private JButton runButton;

	public Segment() {
		RUNNING_STATE = "Running segmentation";
		DONE_STATE = "Segmentation done";
	}

	public Component drawContentPane() {
		runButton = new JButton("Run");
		runButton.setActionCommand("SEGMENT");
		runButton.addActionListener(this);

		return runButton;
	}

	public void setEnable(boolean enable) {
		if (runButton != null)
			runButton.setEnabled(enable);
	}

	// CHECK WHO SENT THE EVENT
	public void actionPerformed(ActionEvent event) {
		String command = event.getActionCommand();

		if (command.equals("SEGMENT")) {
			DataManager jsData = DataManager.getInstance();
			jsData.updateProxy();
			runMethod();
		}
	}

	protected void runAlgorithm() {
		Thread segmentationThread = new Thread(this);
		segmentationThread.start();
	}

	public void run() {

		Calendar calendario = new GregorianCalendar();
		System.out.println("Start: " + calendario.get(Calendar.HOUR) + ":"
				+ calendario.get(Calendar.MINUTE) + ":"
				+ calendario.get(Calendar.SECOND) + ":"
				+ calendario.get(Calendar.MILLISECOND));

		StatusManager.getInstance().setState(RUNNING_STATE);

		MethodManager jsMethodManager = MethodManager.getInstance();
		jsMethodManager.setEnable(false);

		JSMethodProxy jsProxy = JSMethodProxy.getInstance();

		int[] dimExtent = new int[3];
		dimExtent[0] = jsProxy.getHeight();
		dimExtent[1] = jsProxy.getWidth();
		dimExtent[2] = jsProxy.getDepth();

		short[] initialPoints = jsProxy.getInitialPoints();

		JSegmentation.segment(jsProxy.getHeight(), jsProxy.getWidth(), jsProxy
				.getDepth(), jsProxy.getNumberOfImages(), jsProxy
				.getNumberOfRegions(), jsProxy.getImages(), jsProxy.getMask(),
				initialPoints, jsProxy.getSmoothCost(), jsProxy
						.getExpandIterations(), jsProxy.getGraphIterations(),
				jsProxy.getLogFlag());

		ModelImage destImage = new ModelImage(ModelStorageBase.DOUBLE,
				dimExtent, "Segmentation");

		for (int i = 0; i < jsProxy.getHeight() * jsProxy.getWidth()
				* jsProxy.getDepth(); i++) {
			initialPoints[i] = (short) (initialPoints[i] + 1);
		}

		try {
			destImage.importData(0, initialPoints, true);
			ViewJFrameImage frame = new ViewJFrameImage((ModelImage) destImage);
			frame.setVisible(true);
		} catch (IOException e) {
			e.printStackTrace();
		}

		jsMethodManager.setEnable(true);
		StatusManager.getInstance().setState(DONE_STATE);

		Calendar calendario2 = new GregorianCalendar();
		System.out.println("End: " + calendario2.get(Calendar.HOUR) + ":"
				+ calendario2.get(Calendar.MINUTE) + ":"
				+ calendario2.get(Calendar.SECOND) + ":"
				+ calendario2.get(Calendar.MILLISECOND));

		long diff = calendario2.getTimeInMillis()
				- calendario.getTimeInMillis();
		Calendar diferencia = new GregorianCalendar();
		diferencia.setTimeInMillis(diff);
		System.out.println("Time: " + diferencia.get(Calendar.MINUTE) + "min. "
				+ diferencia.get(Calendar.SECOND) + "sec. "
				+ diferencia.get(Calendar.MILLISECOND) + "millisec.");

	}
}
